package com.misty.api.service.email;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.misty.api.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;

/**
 * @ClassName : EmailService
 * @Description : 邮件服务
 * @Author :
 * @Date: 2022-12-23 14:03
 */
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private DefaultKaptcha captchaProducer;

    @Value("${spring.mail.username}")
    private String fromEmail;

    //template模板引擎
    @Autowired
    private TemplateEngine templateEngine;

     /**
      * @param from 发送方
      * @param to 接收方
      * @param subject 主题
      * @param text 内容
      * @return
      * @Description  发送邮件
      * @Date 2022/12/23 14:05
      */
    @Override
    @Async
    public void sendSimpleMail(String from, String to, String subject, String text) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }

    @Override
    @Async
    public void sendRegisterMail(String to, String subject, String text) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }

    @Override
    public void sendHtmlEmail(String to, String subject, String text) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(subject); // 邮件的标题
        helper.setFrom(fromEmail); // 发送者
        helper.setTo(to);    // 接受者
        helper.setSentDate(new Date()); //时间

        // 这里引入的是Template的Context
        Context context = new Context();
        //设置模板中的变量
        String code = captchaProducer.createText();
        log.info("=============邮箱验证码{}发送开始！===========",code);
        context.setVariable("verifyCode", Arrays.asList(code.split("")));
        // 第一个参数为模板的名称
        String process = templateEngine.process("email.html", context); //这里不用写全路径
        // 第二个参数true表示这是一个html文本
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);

    }

}
